/*******************************************************************************
 **  Name: main.cpp                                                           **
 **  Description:                                                             **
 **                                                                           **
 **  CDDL: Open Source Initiative (OSI) Approved License                      **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  The contents of this file are subject to the terms of the CDDL:          **
 **  Common Development and Distribution License (the "License").             **
 **  You may not use this file except in compliance with the License.         **
 **                                                                           **
 **  You can obtain a copy of the license at $PROJECT_ROOT/LICENSE            **
 **  or http://www.opensolaris.org/os/licensing.  This code is Open Source    **
 **  and you are free to use it within the confines of the license, even      **
 **  for your own closed-source commercial projects, as long as you follow    **
 **  the terms set forth in the CDDL.                                         **
 **                                                                           **
 **  When distributing Covered Code, include this CDDL HEADER in each         **
 **  file and include the License file at $PROJECT_ROOT/LICENSE.              **
 **  If applicable, add the following below this CDDL HEADER, with the        **
 **  fields enclosed by brackets "[]" replaced with your own identifying      **
 **  information: Portions Copyright [yyyy] [name of copyright owner]         **
 **                                                                           **
 ******************************************************************************/


#include <iostream>
using namespace std;
#include <unistd.h>


#include "core.h"
#include "droid.h"
#include "db.h"
#include "shell.h"

#define DCON_ID		"$Id$"
#ifndef DCON_VER
#define DCON_VER	"1.0"
#endif

void	Usage();
void	Version();

int main(int argc, char * argv[])
{
    int c;
    bool verbose = false;
    extern char * optarg;
    extern int optind, optopt;


    while((c = getopt(argc,argv,"hVv")) != -1) {
        switch(c) {
        case 'h':
            Usage();
            exit(0);
        case 'V':
            verbose = true;
            break;
        case 'v':
            Version();
            exit(0);
        case ':':
            cout << "That option requires an argument." << endl;
            exit(1);
        case '?':
            cerr << "Unrecognized option: " << (char)optopt << endl;
            exit(1);
        }

    }

    Version();

    DataBase    db(DB_MANIFEST);
    Shell       shell(db);

    cout << "> Reading " << db.Name() << endl;
    if (!db.Open()) {
        cout << "[ERROR] Can't open db: " << db.ErrorMsg() << endl;
        exit(2);
    }


    if (!db.IsConfigured()) {
        cout << "[ERROR] Cannot read Battle Droid configuration." << endl;
        cout << "[ERROR] Have you created an account and registered your droid" << endl;
        cout << "[ERROR] online yet?  http://www.BattleDroids.net/" << endl;
        cout << endl;
        exit(3);
    }

    if (!shell.Connect()) {
        cout << "[ERROR] Connection failure!" << endl;
        cout << "[ERROR] Make sure your AVR is on and connected." << endl;
        cout << "[ERROR] http://www.BattleDroids.net/FAQ for tips" << endl;
        cout << endl;
        exit(1);
    }

    while(shell.Logon() == false);

    shell.Loop();

    db.Close();

    return 0;
}

void	Usage()
{
    Version();

    cout << "Usage: dcon [-hVv] [-p <port>]" << endl;
    cout << "Where:" << endl;
    cout << "       -h          Prints this help" << endl;
    cout << "       -d <file>   Dump droid config to <file>" << endl;
    cout << "       -V          Turns on verbose(debug)" << endl;
    cout << "       -v          Prints version of this client" << endl;
    cout << "       -p <port>   Use serial port <port>" << endl;
    cout << "Ex: dcon -p com1 (Windows)" << endl;
    cout << "Ex: dcon -p /dev/term/0 (Solaris)" << endl;
    cout << "Ex: dcon -p /dev/ttyS0 (Linux)" << endl;
}

void	Version()
{
    cout << endl;
    cout << "** Battle Droid Console v" << DCON_VER << endl;
    cout << "** Copyright 2010 B. Gian James" << endl;
    cout << "** http://www.BattleDroids.net/" << endl;
    cout << "** A tool to log into and manage your droid." << endl;
    cout << endl;
}
